package com.mall.tag;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;

import com.mall.ibatis.bean.IFormat;
import com.mall.util.StringUtil;

@SuppressWarnings("serial")
public class SelectControl extends TagSupport
{
	public final static String pleaseSelect = " -- 请选择 -- ";
	
	private List<IFormat> value;
	private String id;
	private String className;
	private String name;
	private String extend;
	private String disabled;
	private Boolean isNeedDefault; 

	

	public Boolean getIsNeedDefault() {
		return isNeedDefault;
	}


	public void setIsNeedDefault(Boolean isNeedDefault) {
		this.isNeedDefault = isNeedDefault;
	}


	public List<IFormat> getValue() {
		return value;
	}


	public void setValue(List<IFormat> value) {
		this.value = value;
	}


	public String getId()
	{
		return id;
	}


	public void setId(String id)
	{
		this.id = id;
	}


	public String getClassName()
	{
		return className;
	}


	public void setClassName(String className)
	{
		this.className = className;
	}


	public String getName()
	{
		return name;
	}


	public void setName(String name)
	{
		this.name = name;
	}


	public String getExtend()
	{
		return extend;
	}


	public void setExtend(String extend)
	{
		this.extend = extend;
	}


	@Override
	public int doStartTag() throws JspException
	{
		// TODO Auto-generated method stub
		try
		{
			JspWriter out = pageContext.getOut();
			
			String tempString = "<select ${id} ${name} ${className} ${disabled} ${extend}>${options}</select>";
			tempString = (id==null?tempString.replaceAll("\\$\\{id\\}", ""):tempString.replaceAll("\\$\\{id\\}", "id=\""+id+"\""));
			tempString = (name==null?tempString.replaceAll("\\$\\{name\\}", ""):tempString.replaceAll("\\$\\{name\\}", "name=\""+name+"\""));
			tempString = (className==null?tempString.replaceAll("\\$\\{className\\}", ""):tempString.replaceAll("\\$\\{className\\}", "class=\""+className+"\""));
			tempString = ((disabled==null||!disabled.equals("Y")) ?tempString.replaceAll("\\$\\{disabled\\}", ""):tempString.replaceAll("\\$\\{disabled\\}", "disabled=\"disabled\""));
			tempString = (extend==null?tempString.replaceAll("\\$\\{extend\\}", ""):tempString.replaceAll("\\$\\{extend\\}", extend));
			
			StringBuffer options = new StringBuffer();
			if(isNeedDefault)
			{
				options.append("<option value=\"\">"+pleaseSelect+"</option>");
			}
			for(IFormat option:value)
			{
				options.append("<option value=\""+option.optionValue()+"\" title=\""+StringUtil.null2Empty(option.optionTitle(), option.optionText())+"\">"+option.optionText()+"</option>");
			}
			
			tempString = tempString.replaceAll("\\$\\{options\\}", options.toString());
			out.print(tempString);
			out.flush();
		}
		catch (Exception e)
		{
			// TODO: handle exception
			e.printStackTrace();
			
			throw new JspTagException(e.getMessage());
		}
		
		return SKIP_BODY;
	}


	public String getDisabled() {
		return disabled;
	}


	public void setDisabled(String disabled) {
		this.disabled = disabled;
	}

}
